﻿using System;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace DL
{
    [CustomEditor(typeof(GameBlackBoardData))]   
    public class GameBlackboardDataEditor : Editor
    {
        private ReorderableList m_EntryList; 

        private void OnEnable()
        {
            m_EntryList = new ReorderableList(serializedObject, serializedObject.FindProperty("entries"), true, true, true, true)
            {
                drawHeaderCallback = rect =>
                {
                    EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width * .3f, EditorGUIUtility.singleLineHeight), "Key");
                    EditorGUI.LabelField(new Rect(rect.x + rect.width * .3f + 10, rect.y,rect.width * .3f, EditorGUIUtility.singleLineHeight), "Type");
                    EditorGUI.LabelField(new Rect(rect.x + rect.width * .6f + 5, rect.y, rect.width * .4f,EditorGUIUtility.singleLineHeight), "Value");
                },
                
                drawElementCallback = (rect, index, active, focused) =>
                {
                    var elem = m_EntryList.serializedProperty.GetArrayElementAtIndex(index);
                    rect.y += 2;
                    var keyName = elem.FindPropertyRelative("keyName");
                    var valueTypel = elem.FindPropertyRelative("valueTypel");
                    var value = elem.FindPropertyRelative("value");

                    var keyNameRect = new Rect(rect.x, rect.y, rect.width * .3f, EditorGUIUtility.singleLineHeight);
                    var valueTypelRect = new Rect(rect.x + rect.width * .3f, rect.y, rect.width * .3f,
                        EditorGUIUtility.singleLineHeight);
                    var valueRect = new Rect(rect.x + rect.width * .6f, rect.y, rect.width * .4f,
                        EditorGUIUtility.singleLineHeight);

                    EditorGUI.PropertyField(keyNameRect, keyName, GUIContent.none);
                    EditorGUI.PropertyField(valueTypelRect, valueTypel, GUIContent.none);

                    //特殊处理枚举  
                    switch ((AnyValue.ValueType)valueTypel.enumValueIndex)
                    {
                        case AnyValue.ValueType.Int:
                            var intValue = value.FindPropertyRelative("intValue");
                            EditorGUI.PropertyField(valueRect, intValue, GUIContent.none);
                            break;
                        case AnyValue.ValueType.Float:
                            var floatValue = value.FindPropertyRelative("floatValue");
                            EditorGUI.PropertyField(valueRect, floatValue, GUIContent.none);
                            break;
                        case AnyValue.ValueType.Bool:
                            var boolValue = value.FindPropertyRelative("boolValue");
                            EditorGUI.PropertyField(valueRect, boolValue, GUIContent.none);
                            break;
                        case AnyValue.ValueType.String:
                            var stringValue = value.FindPropertyRelative("stringValue");
                            EditorGUI.PropertyField(valueRect, stringValue, GUIContent.none);
                            break;
                        case AnyValue.ValueType.Vector3:
                            var vector3Value = value.FindPropertyRelative("vector3Value");
                            EditorGUI.PropertyField(valueRect, vector3Value, GUIContent.none);
                            break;
                        case AnyValue.ValueType.Vector2:
                            var vector2Value = value.FindPropertyRelative("vector2Value");
                            EditorGUI.PropertyField(valueRect, vector2Value, GUIContent.none);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
            };
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            
            m_EntryList.DoLayoutList();
            //应用更改
            serializedObject.ApplyModifiedProperties();

        }
    }
}